package org.arain.baseii.system.dictionary;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.aspect.annotation.SystemExceptionLog;
import org.arain.baseii.aspect.annotation.SystemServiceLog;
import org.arain.baseii.common.CommonServiceImpl;
import org.arain.baseii.exception.UseException;
import org.arain.baseii.grid.GridData;
import org.arain.baseii.grid.QueryParam;
import org.arain.baseii.mapper.SysDictionaryMapper;
import org.arain.baseii.pojo.SysDictionary;
import org.arain.baseii.pojo.SysDictionaryExample;
import org.arain.baseii.pojo.SysDictionaryExample.Criteria;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.DictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;

/**
 * 
* <p>Title:DictionaryServiceImpl </p>
* @author Arain
* @date2017年9月5日
 */
@Service
@Transactional
public class DictionaryServiceImpl extends CommonServiceImpl<DictionaryDto> implements DictionaryService{
	@Autowired
	private SysDictionaryMapper sysDictionaryMapper;
	@Override
	public String load_grid(QueryParam param) {
		DictionaryDto dto = new DictionaryDto();
		try {
			dto = this.jsonToObject(param.getFilterRules(), dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		SysDictionaryExample example = new SysDictionaryExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(param.getOrder()) && StringUtils.isNotBlank(param.getSort())) {
			example.setOrderByClause(param.getSort()+" "+param.getOrder());
		}
		if(StringUtils.isNotBlank(dto.getType())) {
			criteria.andTypeLike("%"+dto.getType()+"%");
		}
		if(StringUtils.isNotBlank(dto.getName())) {
			criteria.andNameLike("%"+dto.getName()+"%");
		}
		if(StringUtils.isNotBlank(dto.getEnabled())) {
			criteria.andEnabledEqualTo(Integer.valueOf(dto.getEnabled()));
		}
		PageHelper.startPage(param.getPage(), param.getRows());
		JSONArray array = new JSONArray();
		List<SysDictionary> list = sysDictionaryMapper.selectByExample(example);
		int countByExample = sysDictionaryMapper.countByExample(example);
		for (SysDictionary sysDictionary : list) {
			JSONObject object = new JSONObject();
			object.put("type_", sysDictionary.getType()+","+sysDictionary.getValue());
			object.put("type", sysDictionary.getType());
			object.put("name", sysDictionary.getName());
			object.put("value", sysDictionary.getValue());
			object.put("display", sysDictionary.getDisplay());
			object.put("sort", sysDictionary.getSort());
			object.put("enabled", DictUtil.load_dict(sysDictionary.getEnabled()+"", "ENABLED_").getDispaly());
			
			array.add(object);
		}
		GridData data = new GridData();
		data.setTotal(countByExample);
		data.setRows(array);
		return GridData.gridToJson(data);
	}
	@Override
	public DictionaryDto load_01(DictionaryDto dto) {
		String[] split = dto.getType_().split(",");
		SysDictionaryExample example = new SysDictionaryExample();
		example.createCriteria().andTypeEqualTo(split[0]).andValueEqualTo(split[1]);
		List<SysDictionary> list = sysDictionaryMapper.selectByExample(example);
		if(list != null && list.size()>0) {
			AppUtils.copyProperties(dto, list.get(0));
		}
		return dto;
	}
	
	@SystemExceptionLog(description="保存")
	@SystemServiceLog(description="保存")
	@Override
	public ArainResult save_01(DictionaryDto dto) {
		if(StringUtils.isNotBlank(dto.getType_())) {
			String[] split = dto.getType_().split(",");
			SysDictionaryExample example = new SysDictionaryExample();
			example.createCriteria().andTypeEqualTo(split[0]).andValueEqualTo(split[1]);
			List<SysDictionary> list = sysDictionaryMapper.selectByExample(example);
			if(list == null || list.size()<0) {
				throw new UseException("信息错误");
			} 
			SysDictionary dictionary = list.get(0);
			AppUtils.copyProperties(dictionary, dto);
			sysDictionaryMapper.updateByPrimaryKey(dictionary);
			return ArainResult.ok();
			
		} else {
			SysDictionary dictionary = new SysDictionary();
			AppUtils.copyProperties(dictionary, dto);
			sysDictionaryMapper.insert(dictionary);
			return ArainResult.ok();
		}
	}

}
